package com.example.ziru.controller;

import com.example.ziru.entity.Tenant;
import com.example.ziru.service.TenantService;
import com.example.ziru.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/tenant")
@CrossOrigin
public class TenantController {
    @Autowired
    private TenantService tenantService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest request, String tPhone, String tPassword){
        if(!StringUtils.hasLength(tPhone)){
            return new Result("-1","用户名为空",null);
        }
        if(!StringUtils.hasLength(tPassword)){
            return new Result("-1","密码为空",null);
        }

        Tenant tenant = tenantService.selectBytPhone(tPhone);
        if(tenant.getTPassword().equals(tPassword)){
            HttpSession session = request.getSession();
            session.setAttribute("tenant",tenant);
            return new Result("200","登录成功",tenant);
        }else {
            return new Result("-2","账号或密码错误",null);
        }

    }

    //注册
    @RequestMapping("/reg")
    @ResponseBody
    public Object reg(String tNickname,String tPhone,String tPassword1,String tPassword2){
        if(!StringUtils.hasLength(tNickname) || !StringUtils.hasLength(tPhone) || !StringUtils.hasLength(tPassword1)|| !StringUtils.hasLength(tPassword2)){
            return new Result("-1","参数为空",null);
        }
        if(!tPassword1.equals(tPassword2)){
            return new Result("-2","密码不一致",null);
        }
        int i = tenantService.reg(tNickname,tPhone,tPassword1);
        if(i == 0){
            return new Result("-3","注册失败",null);
        }
        return new Result("200","注册成功",null);

    }
}
